﻿#include "Thread.h"//需要修改附加包含目录的内容
#include "xplaneConnect.h" //插件的头文件

//定义数据共享结构体
DashboardData sharedData;

// 数据获取线程函数
void xplaneDataThread() {

    const char* IP = MYIP;      // IP Address of computer running X-Plane
    XPCSocket sock = openUDP(IP);

    // 定义所有需要获取的 DREF 路径（按顺序）
    const char* drefs[] = {
        "sim/flightmodel/position/theta",          // pitch
        "sim/flightmodel/position/phi",            // roll
        "sim/cockpit2/gauges/indicators/altitude_ft_pilot",  // heightAM
        "sim/flightmodel/position/indicated_airspeed",       // airspeed
        "sim/flightmodel/position/vh_ind_fpm",         // verticalSpeed
        "sim/cockpit2/gauges/indicators/turn_rate_heading_deg_pilot",  // turn_rate
        "sim/cockpit2/gauges/indicators/slip_deg", // turn_rate2
        "sim/cockpit2/gauges/indicators/heading_electric_deg_mag_pilot", // heading
        "sim/cockpit/radios/nav1_hdef_dot",        // vor1hdef
        "sim/cockpit/radios/nav2_hdef_dot",        // vor2hdef
        "sim/cockpit/radios/nav1_fromto",          // vor1dr
        "sim/cockpit/radios/nav2_fromto",          // vor2dr
        "sim/flightmodel/engine/ENGN_oil_temp_c",  // Oilc
        "sim/flightmodel/engine/ENGN_oil_press_psi", // Oilp
        "sim/flightmodel/engine/ENGN_bat_amp",     // I
        "sim/cockpit/misc/vacuum",                 // pressure
        "sim/flightmodel/weight/m_fuel1",          // left
        "sim/flightmodel/weight/m_fuel2",          // right
        "sim/flightmodel/engine/ENGN_FF_",         // engine
        "sim/cockpit/radios/nav1_vdef_dot",         // vor1vdf
        "sim/flightmodel/engine/ENGN_tacrad"// engineRMB
    };

    float* values[21];

    unsigned char count = 21;

    int sizes[21];

    for (int i = 0; i < 21; i++)
    {
        sizes[i] = 1;
        values[i] = (float*)malloc(1 * sizeof(float));

    }
    for (; is_run();) {
        {
            std::lock_guard<std::mutex> lock(sharedData.mtx);

            // 调用 getDREFs 函数
            int result = getDREFs(sock, drefs, values, count, sizes);

            // 错误处理
            if (result < 0) {
                sock = openUDP(IP);  // 重新连接
            }
            else {
                // 解析数据（需严格对应 drefs 顺序）
                sharedData.pitch = -values[0][0];
                sharedData.roll = (-values[1][0]) * static_cast<float>(M_PI) / 180.0f;
                sharedData.heightAM = values[2][0];
                sharedData.airspeed = values[3][0];
                sharedData.verticalSpeed = values[4][0];
                sharedData.turn_rate = values[5][0];
                sharedData.turn_rate2 = values[6][0];
                sharedData.heading = values[7][0];
                sharedData.vor1hdef = values[8][0];
                sharedData.vor2hdef = values[9][0];
                sharedData.vor1dr = values[10][0];
                sharedData.vor2dr = values[11][0];
                sharedData.Oilc = values[12][0];
                sharedData.Oilp = values[13][0];
                sharedData.I = values[14][0];
                sharedData.pressure = values[15][0];
                sharedData.left = values[16][0];
                sharedData.right = values[17][0];
                sharedData.engine = values[18][0];
                sharedData.vor1vdf = values[19][0];
                sharedData.engineRPM = values[20][0];
            }
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(5));
    }
}

// 将未完成的版本注释
//
//// 姿态仪数据更新
//void updateAttitudeIndicator() {
//    std::lock_guard<std::mutex> lock(sharedAttitudeIndicatorData.mtx);
//    getDREF(sock, "sim/flightmodel/position/theta", &sharedAttitudeIndicatorData.pitch, &sizepc);
//    getDREF(sock, "sim/flightmodel/position/phi", &sharedAttitudeIndicatorData.roll, &sizepc);
//    // 数据处理（角度转换）
//    sharedAttitudeIndicatorData.pitch = -sharedAttitudeIndicatorData.pitch;
//    sharedAttitudeIndicatorData.roll = (-sharedAttitudeIndicatorData.roll) * static_cast<float>(M_PI) / 180.0f;
//}
//
//// 高度表数据更新
//void updateAltimeter() {
//    std::lock_guard<std::mutex> lock(sharedAltimeterData.mtx);
//    getDREF(sock, "sim/cockpit2/gauges/indicators/altitude_ft_pilot", &sharedAltimeterData.heightAM, &sizeheight);
//}
//
//// 空速表数据更新
//void updateAirspeed() {
//    std::lock_guard<std::mutex> lock(sharedAirspeedIndicatorData.mtx);
//    getDREF(sock, "sim/flightmodel/position/indicated_airspeed", &sharedAirspeedIndicatorData.airspeed, &sizeas);
//}
//
//// 垂直速度表数据更新
//void updateVerticalSpeed() {
//    std::lock_guard<std::mutex> lock(sharedVerticalSpeedIndicatorData.mtx);
//    getDREF(sock, "sim/flightmodel/velocity/vh_ind", &sharedVerticalSpeedIndicatorData.verticalSpeed, &sizevs);
//}
//
//// 转弯协调仪数据更新
//void updateTurnCoordinator() {
//    std::lock_guard<std::mutex> lock(sharedTurnCoordinatorData.mtx);
//    getDREF(sock, "sim/cockpit2/gauges/indicators/turn_rate_heading_deg_pilot", &sharedTurnCoordinatorData.turn_rate, &sizetr);
//    getDREF(sock, "sim/cockpit2/gauges/indicators/slip_deg", &sharedTurnCoordinatorData.turn_rate2, &sizetr2);
//}
//
//// 航向仪数据更新
//void updateHeadingIndicator() {
//    std::lock_guard<std::mutex> lock(sharedHeadingIndicatorData.mtx);
//    getDREF(sock, "sim/flightmodel/position/true_psi", &sharedHeadingIndicatorData.heading, &sizehg);
//}
//
//// VOR1导航仪数据更新
//void updateVOR1() {
//    std::lock_guard<std::mutex> lock(sharedVor1Data.mtx);
//    getDREF(sock, "sim/cockpit/radios/nav1_hdef_dot", &sharedVor1Data.vor1hdef, &sizedf1);
//    getDREF(sock, "sim/cockpit/radios/nav1_fromto", &sharedVor1Data.vor1dr, &sizedr1);
//}
//
//// VOR2导航仪数据更新
//void updateVOR2() {
//    std::lock_guard<std::mutex> lock(sharedVor2Data.mtx);
//    getDREF(sock, "sim/cockpit/radios/nav2_hdef_dot", &sharedVor2Data.vor2hdef, &sizedf2);
//    getDREF(sock, "sim/cockpit/radios/nav2_fromto", &sharedVor2Data.vor2dr, &sizedr2);
//}
//
//// 鼠标数据更新
//void updateMouseData() {
//    std::lock_guard<std::mutex> lock(sharedMouseData.mtx);
//    sharedMouseData.msg = getmouse();
//}
//
//// 发动机数据更新（油温油压）
//void updateEngineData() {
//    std::lock_guard<std::mutex> lock(sharedEngineData.mtx);
//    getDREF(sock, "sim/flightmodel/engine/ENGN_OIL_TEMP_deg_C", &sharedEngineData.Oilc, &sizedf1);
//    getDREF(sock, "sim/flightmodel/engine/ENGN_OIL_PRESS_psi", &sharedEngineData.Oilp, &sizedf2);
//}
//
//// 电气系统数据更新
//void updateElectricalData() {
//    std::lock_guard<std::mutex> lock(sharedElectricalData.mtx);
//    getDREF(sock, "sim/cockpit2/electrical/bus_current_amps", &sharedElectricalData.I, &sizedr1);
//    getDREF(sock, "sim/cockpit2/electrical/bus_voltage", &sharedElectricalData.pressure, &sizedr2);
//}
//
//void xplaneDataThread() {
//    // 初始化socket
//    const char* IP = MYIP;
//    XPCSocket sock = openUDP(IP);
//
//    while (true) {
//        std::vector<std::thread> threads;
//
//        // 并行启动所有更新任务
//        threads.emplace_back(updateAttitudeIndicator);
//        threads.emplace_back(updateAltimeter);
//        threads.emplace_back(updateAirspeed);
//        threads.emplace_back(updateVerticalSpeed);
//        threads.emplace_back(updateTurnCoordinator);
//        threads.emplace_back(updateHeadingIndicator);
//        threads.emplace_back(updateVOR1);
//        threads.emplace_back(updateVOR2);
//        threads.emplace_back(updateMouseData);
//        threads.emplace_back(updateEngineData);
//        threads.emplace_back(updateElectricalData);
//
//        // 等待所有线程完成
//        for (auto& t : threads) t.join();
//
//        // 控制更新频率
//        std::this_thread::sleep_for(std::chrono::milliseconds(50));
//    }
//}